# -*- mode: python; -*-

#
# Copyright (c) 2013 Juniper Networks, Inc. All rights reserved.
#

import os
import sys

Import('BuildEnv')
PartEnv = BuildEnv.Clone()

setup_sources = [
           'setup.py',
           'MANIFEST.in',
          ]

setup_sources_rules = []
for file in setup_sources:
    setup_sources_rules.append(
        PartEnv.Install(Dir("."), '#src/contrail-common/libpartition/' + file))

local_sources = [
           '__init__.py',
           'libpartition.py',
           'consistent_hash.py',
          ]

local_sources_rules = []
for file in local_sources:
    local_sources_rules.append(PartEnv.Install(Dir("libpartition"), file))

sdist_depends = [setup_sources_rules, local_sources_rules]

cd_cmd = 'cd ' + Dir('.').path + ' && '
sdist_gen = PartEnv.Command(
    'dist/libpartition-%s.tar.gz' % PartEnv.GetPyVersion(),
    'setup.py',
    cd_cmd + 'python setup.py sdist')
# install everything before building distribution
PartEnv.Depends(sdist_gen, sdist_depends)

def SymLink(target, source, env):
    os.symlink(os.path.abspath(str(source[0])), os.path.abspath(str(target[0])))

if 'install' in BUILD_TARGETS:
    install_cmd_py2 = PartEnv.Command(None,
                                      'setup.py',
                                      cd_cmd + 'python setup.py install %s' %
                                      PartEnv['PYTHON_INSTALL_OPT'])
    PartEnv.Depends(install_cmd_py2, sdist_depends)
    PartEnv.Alias('install', install_cmd_py2)

    setup_link_cmd = PartEnv.Command("setup-py3.py", "setup.py", SymLink)
    PartEnv.Depends(setup_link_cmd, sdist_depends)
    install_cmd_py3 = PartEnv.Command(None,
                                      'setup-py3.py',
                                      cd_cmd + 'python3 setup-py3.py install %s' %
                                      PartEnv['PYTHON_INSTALL_OPT'])
    PartEnv.Depends(install_cmd_py3, sdist_depends)
    PartEnv.Alias('install', install_cmd_py3)

PartEnv['LIBPARTITION_PKG'] = sdist_gen
#PartEnv.SConscript('test/SConscript', exports='PartEnv', duplicate = 0)
#PartEnv.SConscript('docs/SConscript', exports='PartEnv', duplicate = 0)

PartEnv.Default(sdist_gen)
PartEnv.Alias("contrail:libpartition", sdist_gen)
